package com.hafu.swing;

import com.hafu.model.Teacher;
import com.hafu.service.TeacherService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// 添加教师对话框
public class AddTeacherDialog extends JDialog {
    private TeacherService teacherService;
    private JTextField teacherIdField;
    private JTextField nameField;
    private JComboBox<String> genderComboBox;
    private JTextField ageField;
    private JTextField titleField;
    private JTextField departmentField;
    private JTextField phoneField;

    public AddTeacherDialog(JFrame parent, TeacherService teacherService) {
        super(parent, "添加教师", true);
        this.teacherService = teacherService;
        setSize(400, 450);
        setLocationRelativeTo(parent);
        setLayout(new BorderLayout());

        // 表单面板
        JPanel formPanel = new JPanel(new GridLayout(7, 2, 10, 10));
        formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        formPanel.add(new JLabel("教师工号:"));
        teacherIdField = new JTextField();
        formPanel.add(teacherIdField);

        formPanel.add(new JLabel("姓名:"));
        nameField = new JTextField();
        formPanel.add(nameField);

        formPanel.add(new JLabel("性别:"));
        genderComboBox = new JComboBox<>(new String[]{"男", "女"});
        formPanel.add(genderComboBox);

        formPanel.add(new JLabel("年龄:"));
        ageField = new JTextField();
        formPanel.add(ageField);

        formPanel.add(new JLabel("职称:"));
        titleField = new JTextField();
        formPanel.add(titleField);

        formPanel.add(new JLabel("所属院系:"));
        departmentField = new JTextField();
        formPanel.add(departmentField);

        formPanel.add(new JLabel("联系电话:"));
        phoneField = new JTextField();
        formPanel.add(phoneField);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        JButton saveButton = new JButton("保存");
        JButton cancelButton = new JButton("取消");

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveTeacher();
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        add(formPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void saveTeacher() {
        // 验证输入
        String teacherId = teacherIdField.getText().trim();
        String name = nameField.getText().trim();
        String gender = (String) genderComboBox.getSelectedItem();
        String ageStr = ageField.getText().trim();
        String title = titleField.getText().trim();
        String department = departmentField.getText().trim();
        String phone = phoneField.getText().trim();

        if (teacherId.isEmpty() || name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "教师工号和姓名不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        Integer age = null;
        try {
            if (!ageStr.isEmpty()) {
                age = Integer.parseInt(ageStr);
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "年龄必须是数字", "输入错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        // 创建教师对象
        Teacher teacher = new Teacher(teacherId, name, gender, age, title, department, phone);

        // 保存教师信息
        boolean success = saveTeacherToDatabase(teacher);
        if (success) {
            JOptionPane.showMessageDialog(this, "教师信息保存成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            dispose();
        } else {
            JOptionPane.showMessageDialog(this, "教师信息保存失败", "失败", JOptionPane.ERROR_MESSAGE);
        }
    }

    private boolean saveTeacherToDatabase(Teacher teacher) {
        // 这里应该调用 DAO 层保存教师信息
        // 示例实现
        return true; // 假设保存成功
    }
}